/*
actions 触发mutations
 */
import {
    RECEIVE_ADDRESS,
    RECEIVE_CATEGORYS,
    RECEIVE_SHOPS,
    RECEIVE_USER_INFO,
    RECEIVE_SHOP_SELLER,
    RECEIVE_SHOP_RATINGS,
    RECEIVE_SHOP_GOODS,
    INCREMENT_FOOD_COUNT,
    DECREMENT_FOOD_COUNT,
    CLEAR_SHOPCAR,
    RECEIVE_SEARCH_SHOPS
} from './mutation-types'

import {
    reqAddress,
    reqFoodCategorys,
    reqShops,
    reqUserInfo,
    reqLogout,
    reqShopGoods,
    reqShopRatings,
    reqShopSeller,
    reqSearchShops
} from "../api";

export default {
    // 异步获取address
    async getAddress ({commit,state}) {
        const geohash=state.latitude + ',' +state.longitude
        const result = await reqAddress(geohash)
        if(result.code==0){
            commit(RECEIVE_ADDRESS,{address: result.data})
        }
    },
    // 异步获取receive_categorys
    async getCategorys ({commit}) {
        const result = await reqFoodCategorys()
        if(result.code==0){
            commit(RECEIVE_CATEGORYS,{categorys: result.data})
        }
    },
    // 异步获取receive_shops
    async getShops ({commit,state}) {
        const {latitude,longitude} = state
        const result = await reqShops({latitude,longitude})
        if(result.code==0){
            commit(RECEIVE_SHOPS,{shops: result.data})
        }
    },
    /*
    action 使用 异步还是同步
    看数据 是从接口获取，
          还是已经有了数据
    我们这里已经有了数据 只是保存下
     */
    recodeUserInfo ({commit},userInfo) {
        commit(RECEIVE_USER_INFO,{userInfo})
    },
    /*
    异步获取用户信息
     */
    async getUserInfo ({commit}){
        const result =await reqUserInfo();
        if(result.code==1){
            const userInfo=result;
            commit(RECEIVE_USER_INFO,{userInfo})
        }
    },
    /*
    退出登录
     */
    async reqLogout({commit}){
        const result =await reqLogout();
        console.log(result)
        if(result.code==0){
            const userInfo={};
            commit(RECEIVE_USER_INFO,{userInfo})
        }
    },
    /*
    mock 数据
    获取商家goods
     */
    async reqShopGoods({commit},callback){
      const result =await reqShopGoods();
      if(result.code==0){
        const shopGoods=result.data;
        commit(RECEIVE_SHOP_GOODS,{shopGoods})
        callback && callback();
      }
    },
    /*
      mock 数据
      获取商家ratings
       */
    async reqshopRatings({commit}){
      const result =await reqShopRatings();
      if(result.code==0){
        const shopRatings=result.data;
        commit(RECEIVE_SHOP_RATINGS,{shopRatings})
      }
    },
    /*
        mock 数据
        获取商家Seller
         */
    async reqshopSeller({commit}){
      const result =await reqShopSeller();
      if(result.code==0){
        const shopSeller=result.data;
        commit(RECEIVE_SHOP_SELLER,{shopSeller})
      }
    },

    /*
    同步更新 food的数量
     */
    updataFoodCunt ({commit},{isAdd,food}){
      if(isAdd){
        // 增加数量
        commit(INCREMENT_FOOD_COUNT,{food})
      }else{
        //减少数量
        commit(DECREMENT_FOOD_COUNT,{food})
      }
    },
    /*
      同步清除购物车
     */
    clearShopCar({commit},callback){
      commit(CLEAR_SHOPCAR)
      callback && callback();
    },
    // 异步获取searchShops
    async searchShops ({commit,state},keyword) {
        const geohash=state.latitude + ',' +state.longitude
        const result = await reqSearchShops({geohash,keyword})
        console.log('actions:',geohash,keyword)
        if(result.code==0){
            commit(RECEIVE_SEARCH_SHOPS,{searchShops:result.data})
        }
    },
}
